<?php

/**
 * Bridge Controller
 *
 */

class BridgeController extends Bridge_Abstract_Controller
{
	private $cache; // database cache
	private $worldcat; // worldcat search object
	private $libraries; // libraries to limit to
	
	public function __construct($config)
	{
		parent::__construct($config);
		
		// get all library files
		
		require_once ("lib/Cache.php");
		require_once ("lib/Z3950.php");
		require_once ("lib/Innopac.php");
		require_once ("lib/Voyager.php");
		require_once ("lib/Marc.php");
		require_once ("lib/Record.php");		
		require_once ("lib/WorldCat.php");
		
		// get list of libraries from config
				
		$arrLibraries = $this->config->xpath("//library/@oclc");
		$arrCodes = array();
		
		foreach ( $arrLibraries as $library )
		{
			array_push($arrCodes, (string) $library);
		}
		
		$this->libraries = implode(",", $arrCodes);
		
		// worldcat search object
		
		$key = (string) $this->config->worldcat_key;
		$this->worldcat = new Mango_WorldCat($key);
		
		// cache

		$this->cache = new Bridge_Cache($this->config);
		
		// configs to add to response

		$this->response->add($this->config->libraries);
		$this->response->add($this->config->link_resolver);		
	}
	
	public function execute()
	{
		try
		{
			$format = $this->request->getAttribute("format");
			$oclc = $this->request->getAttribute("oclc", true);
			
			// get action, if not supplied then 'index'
			
			$action = $this->request->getAttribute("action", false, "index");
			
			// if specified, but not defined here
			
			if ( ! method_exists($this, $action) )
			{
				throw new Exception("unsupported action");
			}
			
			// do it!
			
			$this->$action();
			
			if ( $format == "xml")
			{
				header("Content-type: text/xml");
				echo $this->response->saveXML();
			}
			elseif ( $this->view != "" )
			{
				echo $this->transform->execute($this->response, $this->view, true);
			}
			
		}
		catch ( Exception $e )
		{
			echo "<h1 style='color:red'>Error</h1>";
			echo "<p>" . $e->__toString() . "</p>";			
		}
		
		return null;
	}
	
	protected function index()
	{
		$oclc = $this->request->getAttribute("oclc", true);
		
		$record = $this->getRecord($oclc);
		
		$this->response->add($record->toXML());
		$this->response->add($record->getMarcXML());
		
		$this->view = "views/index.xsl";
	}
	
	protected function editions()
	{
		$oclc = $this->request->getAttribute("oclc", true);
		
		$document = $this->getEditions($oclc);
		
		foreach ( $document->records() as $record )
		{
			$this->response->add($record->toXML());
		}
		
		$this->view = "views/editions.xsl";
	}
	
	protected function holdings()
	{
		 $oclc = $this->request->getAttribute("oclc", true);
		 
		 $xml = $this->cache->get($oclc, "holdings");
		 
		 if ( $xml == null )
		 {
		 	$this->worldcat->limitToLibraries($this->libraries);
			
			 $xml = $this->worldcat->holdings($oclc, 1, 50);
			 $this->cache->set($oclc, "holdings", $xml);
		 }
		 
		 $this->response->add($xml);
		 
		 $this->view = "views/holdings.xsl";
	}
	
	protected function lookup()
	{
		$library = $this->request->getAttribute("library", true);
		$oclc = $this->request->getAttribute("oclc", true);
		
		$arrLibraries = $this->config->xpath("//library[@oclc ='$library']");
		
		if ( count($arrLibraries) != 1 )
		{
			throw new Exception("could not find library in config file");
		}
		
		$server = (string) $arrLibraries[0]["z3950"];
		
		$document = $this->getEditions($oclc);

		$query = "";
		
		foreach ( $document->records() as $record )
		{
			foreach ( $record->getAllISBN() as $isbn )
			{
				$query .= "ISBN:$isbn "; 
			}
			
			$oclc_number = $record->getControlNumber();

			if ( $oclc_number != "" )
			{
				$query .= "OCLC:$oclc_number "; 
			}
		}
		
		if ( strstr($server, "innopac") )
		{
			$catalog = new Xerxes_Innopac($server);	
		}
		elseif ( strstr($server, "voyager") )
		{
			$catalog = new Xerxes_Voyager($server);	
		}
		elseif ( strstr($server, "unicorn") )
		{
			$catalog = new Xerxes_Voyager($server);	
		}
		
		$results = $catalog->getAvailability($query);
		
		$this->response->add($results->toXML());
		$this->view = "views/lookup.xsl";
	}
	
	public function resolve()
	{
		$oclc = $this->request->getAttribute("rft.oclc");
		if ( $oclc == "" ) $oclc = $this->request->getAttribute("oclc");
		
		$isbn = $this->request->getAttribute("isbn");
		if ( $isbn == "" ) $isbn = $this->request->getAttribute("rft.isbn");
		
		if ( $oclc != "" )
		{
		
		}
		elseif ( $isbn != "" )
		{
		
		}
		else
		{
			throw new Exception("request must have an ISBN or OCLC number");
		}
	}
	
	private function getRecord($oclc)
	{
		$xml = $this->cache->get($oclc, "record");
		
		if ( $xml == null )
		{
			$xml = $this->worldcat->record($oclc);
			$this->cache->set($oclc, "record", $xml);
		}
		
		$record = new Xerxes_Record();
		$record->loadXML($xml);	
		
		return $record;
	}
	
	private function getEditions($oclc)
	{
		$xml = $this->cache->get($oclc, "editions");
		
		if ( $xml == null )
		{
			$this->worldcat->limitToLibraries($this->libraries);

			$xml = $this->worldcat->getEditions($oclc, "marcxml", "Date", true);
			$this->cache->set($oclc, "editions", $xml);
		}

		$document = new Xerxes_Record_Document();
		$document->loadXML($xml);		
		
		return $document;
	}
}

?>